package com.fcy.teamwork.business.projectlife

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.api.BasicCallback
import com.fcy.teamwork.business.SelectFromGroupDiaFragment
import com.fcy.teamwork.pojo.Person
import com.fcy.teamwork.pojo.PersonProject
import com.fcy.teamwork.pojo.ProjectInfo
import com.fcy.teamwork.pojo.ProjectNode
import com.fcy.teamwork.pojo.TeamWorkResult
import com.fcy.teamwork.repo.RetrofitClient
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query

interface ProjectControlService {
    @GET("sms/project/nodes")
    suspend fun allNodes(@Query("projectId") projectId: String): TeamWorkResult<List<List<ProjectNode>>>

    @POST("sms/project/updateNode")
    suspend fun updateNode(@Body node: ProjectNode): TeamWorkResult<Boolean>

    @GET("sms/project/loadAllMembers")
    suspend fun loadAllMembers(@Query("projectId") projectId: String): TeamWorkResult<List<Person>>

    @POST("sms/project/removeMember")
    suspend fun removeFromGroup(@Body personProject: PersonProject): TeamWorkResult<Any?>
}

class ProjectControllerVM : ViewModel() {
    val buildingBode by lazy {
        ProjectNode(
            0,
            projectInfo.id.toString(),
            null,
            null,
            null,
            0,
            null,
            ""
        )
    }
    private val service: ProjectControlService by lazy { RetrofitClient.getService() }
    lateinit var projectInfo: ProjectInfo

    var selectedMembers: List<Person> = emptyList()
        set(value) {
            field = value
            _selectMemberFlow.tryEmit(field)
        }

    private val _selectMemberFlow: MutableSharedFlow<List<Person>> = MutableSharedFlow(
        extraBufferCapacity = 1
    )
    val selectFlow: SharedFlow<List<Person>> = _selectMemberFlow

    private val _allNodesFlow: MutableSharedFlow<List<List<ProjectNode>>> = MutableSharedFlow(
        extraBufferCapacity = 1
    )
    val allNodesFlow: SharedFlow<List<List<ProjectNode>>> = _allNodesFlow


    suspend fun loadAllMember(groupId: String? = null): List<Person> {
        return service.loadAllMembers(groupId ?: projectInfo.id.toString()).data
    }


    // 拉取当前应用所有信息
    fun loadAllNodes() {
        viewModelScope.launch {
            _allNodesFlow.tryEmit(
                service.allNodes(projectInfo.id.toString()).data
            )
        }
    }

    // 更新节点信息
    fun updateNodeInfo(node: ProjectNode) {
        viewModelScope.launch {
            val res = async { service.updateNode(node) }
            res.await()
            loadAllNodes()
        }
    }


    suspend fun removeFromGroup(personProject: PersonProject) {
        JMessageClient.removeGroupMembers(
            personProject.projectId.toLong(),
            mutableListOf(personProject.personId),
            object : BasicCallback() {
                override fun gotResult(p0: Int, p1: String?) {
                    viewModelScope.launch {
                        service.removeFromGroup(personProject)
                    }
                }
            })
    }

}